package com.abe.abetest.finger;

import android.Manifest;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;

import com.abe.libbase.base.BaseActivity;
import com.abe.abetest.R;

import java.security.KeyStore;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

/*指纹识别需要Android6.0以上且硬件支持*/
public class FingerPrintActivity extends BaseActivity {
    private FingerprintManager manager;
    private KeyStore keyStore;
    private static final String DEFAULT_KEY_NAME = "default_key";

    @Override
    public int getLayoutRes() {
        return R.layout.activity_fingerprint;
    }

    @Override
    public void init() {
        if (Build.VERSION.SDK_INT >= 23) {
            manager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
            if (checkFingerprint()) {
                initKey();
                initCipher();
            }
        }
    }

    private void initKey() {
        if (Build.VERSION.SDK_INT >= 23)
            try {
                keyStore = KeyStore.getInstance("AndroidKeyStore");
                keyStore.load(null);
                KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
                KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(DEFAULT_KEY_NAME,
                        KeyProperties.PURPOSE_ENCRYPT |
                                KeyProperties.PURPOSE_DECRYPT)
                        .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                        .setUserAuthenticationRequired(true)
                        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
                keyGenerator.init(builder.build());
                keyGenerator.generateKey();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
    }

    private void initCipher() {
        if (Build.VERSION.SDK_INT >= 23)
            try {
                SecretKey key = (SecretKey) keyStore.getKey(DEFAULT_KEY_NAME, null);
                Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
                        + KeyProperties.BLOCK_MODE_CBC + "/"
                        + KeyProperties.ENCRYPTION_PADDING_PKCS7);
                cipher.init(Cipher.ENCRYPT_MODE, key);
                showFingerPrintDialog(cipher);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
    }

    private boolean checkFingerprint() {
        if (Build.VERSION.SDK_INT >= 23) {
            // 权限判断
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "没有指纹识别权限", Toast.LENGTH_SHORT).show();
                return false;
            }
            // 硬件判断
            if (!manager.isHardwareDetected()) {
                Toast.makeText(this, "没有指纹识别模块", Toast.LENGTH_SHORT).show();
                return false;
            }
            if (!manager.hasEnrolledFingerprints()) {
                Toast.makeText(this, "您至少需要在系统设置中添加一个指纹", Toast.LENGTH_SHORT).show();
                return false;
            }
            return true;
        }
        return false;
    }

    private void showFingerPrintDialog(Cipher cipher) {
        final FingerPrintDialog fingerPrintDialog = new FingerPrintDialog();
        fingerPrintDialog.setCipher(cipher);
        fingerPrintDialog.setCancelable(false);
        fingerPrintDialog.show(getSupportFragmentManager(), "fingerprint");
    }
}
